from django.shortcuts import render
from django.http import HttpResponse
from .date_move import  Move
import redis
from pyecharts import Bar,Line
import json

moveDoban = Move()
r = redis.Redis(host='172.16.44.57', port=6379, password='1', db='0')
#r = redis.Redis(host='192.168.43.151', port=6379, password='1', db='0')
REMOTE_HOST = "https://pyecharts.github.io/assets/js"
# Create your views here.
def massage_index(request):
    info1 = moveDoban.get_top_five_user_content()
    info2 = moveDoban.every_move_star_small_two_like_stree()
    info3 = moveDoban.Dark_people_move()
    info4 = moveDoban.every_move_max_like()
    connet = {"move_name":info1.loc[:,'Movie_Name_CN'],
               "Comment":info1.loc[:,'Comment'],
               "info3":info3,
               "info4":info4,
              "info2":info2,
    }
    return render(request, 'tab-panel.html', context=connet)
    # return render(request,'move_index.html',context=connet)
#1
def make_most_user_comment_puter(request):

    info =moveDoban.make_most_user_comment()

    bar = Bar('每部电影评论人数')

    bar.add('次数',info.index,info.values,is_datazoom_show=True,is_label_show=True)

    content = dict(

        myechart=bar.render_embed(),

        host=REMOTE_HOST,

        script_list=bar.get_js_dependencies()
    )
    return render(request,'tab-panel.html',content)
#2
def Total_thumb_up_pe_film_sum_puter(request):

    bar = Bar('每部电影的点赞数量总计')

    if r.exists("Movie_Name_CN"):

        info_dic = json.loads(r.get("Movie_Name_CN").decode())

        bar.add('点赞数量', list(info_dic.keys()),list(info_dic.values()), is_datazoom_show=True, is_label_show=True)
    else:

        info = moveDoban.Total_thumb_up_pe_film_sum()

        bar.add("点赞数量",info.index,info.values,is_datazoom_show=True,is_label_show=True)

    content = dict(

        myechart=bar.render_embed(),

        host=REMOTE_HOST,

        script_list=bar.get_js_dependencies()
    )
    return render(request, 'tab-panel.html', content)
    # return render(request, 'date_index.html', content)
#4每个电影的平均评分,并做出bar图
def every_move_star_average_score_puter(request):

    info = moveDoban.every_move_star_average_score()

    bar = Bar('每部电影的平均评分')

    bar.add('平均分', info.index, info.values ,is_datazoom_show=True,is_label_show=True)

    content = dict(

        myechart=bar.render_embed(),

        host=REMOTE_HOST,

        script_list=bar.get_js_dependencies()
    )
    return render(request, 'tab-panel.html', content)
    # return render(request, 'date_index.html', content)
#5为了真实反应电影的平均分数，计算电影平均分时将’Like‘少于100的数据不计算在内，重新计算平均分数
def every_move_star_greater_average_score_puter(request):
    info = moveDoban.every_move_star_greater_average_score()

    bar = Bar('每部电影的真实平均评分')

    bar.add('平均分展示图', info.index, info.values, is_datazoom_show=True, is_label_show=True)

    content = dict(

        myechart=bar.render_embed(),

        host=REMOTE_HOST,

        script_list=bar.get_js_dependencies()
    )
    return render(request, 'tab-panel.html', content)
    #return render(request, 'date_index.html', content)
#6、将发布评论最多的前20个用户及对应的发布量，
def The_top_20_users_with_the_most_comments_puter(request):

    info = moveDoban.The_top_20_users_with_the_most_comments()

    bar = Bar('发布评论最多的前20个用户及对应的发布量')

    bar.add('结果展示图', info.index, info.values, is_datazoom_show=True, is_label_show=True)

    content = dict(

        myechart=bar.render_embed(),

        host=REMOTE_HOST,

        script_list=bar.get_js_dependencies()
    )
    return render(request, 'tab-panel.html', content)
    #return render(request, 'date_index.html', content)
#9、将’复仇者联盟2‘电影的每天评论的数量做成Line图
def The_number_of_daily_reviews_of_the_film_puter(request):

    info = moveDoban.The_number_of_daily_reviews_of_the_film()

    line = Line('联盟2每天评论的数量')

    line.add('结果展示图', info.index, info.values, is_smooth=True, mark_line=["max", "average"])

    content = dict(

        myechart=line.render_embed(),

        host=REMOTE_HOST,

        script_list=line.get_js_dependencies()
    )
    return render(request, 'tab-panel.html', content)
    #return render(request, 'date_index.html', content)
#将每部电影'Like'值最高的前5个评论展示出来
#测试
def make_most_user_comment_puter_test(request):

    bar = Bar('每部电影评论人数')

    if r.exists('Movie_Name_CN'):

        info_dict1 = json.loads(r.get("Movie_Name_CN").decode())

        bar.add('次数',list(info_dict1.keys()),list(info_dict1.values()))

    else:

        info = moveDoban.make_most_user_comment()

        bar.add('次数',info.index,info.values)

    content = dict(

        myechart=bar.render_embed(),

        host=REMOTE_HOST,

        script_list=bar.get_js_dependencies()
    )
    return render(request, 'tab-panel.html', content)
   # return render(request,'date_index.html',content)










